<template class="">
  <div>
    <div class="bar-wrapper">
      <el-form inline size="small">
        <el-form-item label="日期范围">
          <el-date-picker
            v-model="formData.dateRange"
            type="daterange"
            range-separator="To"
            start-placeholder="起始日期"
            end-placeholder="结束日期"
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item label="扇区建议">
          <el-select
            v-model="formData.advice"
            placeholder="扇区建议"
            style="width: 120px"
          >
            <el-option
              v-for="item in adviceOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="双流正常率">
          <el-select
            v-model="formData.rateRangeZUUU"
            placeholder="双流正常率"
            style="width: 120px"
          >
            <el-option
              v-for="item in rateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="天府正常率">
          <el-select
            v-model="formData.rateRangeZUTF"
            placeholder="天府正常率"
            style="width: 120px"
          >
            <el-option
              v-for="item in rateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery()">
            <el-icon><Search /></el-icon>
            <span>检索</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="content-wrapper">
      <div v-if="queryResult.length == 0">
        <el-empty message=""></el-empty>
      </div>
      <div v-else>
        <!-- <pre>{{ queryParameters }}</pre> -->
        <!-- <pre>{{ queryResult }}</pre> -->
        <el-table :data="queryResult" style="width: 100%" size="small">
          <el-table-column type="expand">
            <template #default="props">
              <el-scrollbar width="calc(100% - 20px)" height="300px">
                <div style="display: flex">
                  <pre
                    v-for="fd in props.row.flowData"
                    style="
                      white-space: pre-wrap;
                      word-wrap: break-word;
                      width: 200px;
                      min-width: 200px;
                      max-width: 200px;
                      border-right: 1px dashed #8884;
                    "
                    >{{ JSON.stringify(fd, "", 1) }}
                    </pre
                  >
                  <!-- <pre>{{ rops.row.flowData }}</pre> -->
                </div>
              </el-scrollbar>
            </template>
          </el-table-column>
          <el-table-column
            prop="number"
            label="#"
            min-width="100"
          ></el-table-column>
          <el-table-column
            prop="moment"
            label="时刻"
            min-width="200"
          ></el-table-column>
          <el-table-column
            prop="currentSectorCount"
            label="扇区"
            min-width="100"
          ></el-table-column>
          <el-table-column
            prop="autoAdvice"
            label="建议A"
            min-width="100"
          ></el-table-column>
          <el-table-column
            prop="manualAdvice"
            label="建议M"
            min-width="100"
          ></el-table-column>
          <el-table-column
            prop="adviceDifference"
            label="差异A"
            min-width="100"
          ></el-table-column>
          <el-table-column
            prop="manualDifference"
            label="差异M"
            min-width="100"
          ></el-table-column>
          <el-table-column
            prop="rateZUUU"
            label="ZUUU"
            min-width="100"
          ></el-table-column>
          <el-table-column
            prop="rateZUTF"
            label="ZUTF"
            min-width="100"
          ></el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref, computed, onMounted, h } from "vue";
import axios from "axios";
import dayjs from "dayjs";
import { ElNotification, ElMessage, ElMessageBox } from "element-plus";

const rateOptions = ref([
  {
    value: "0,100",
    label: "不限",
  },
  {
    value: "0,89",
    label: "0% ~ 90%(不含)",
  },
  {
    value: "90,100",
    label: "90%(含) ~ 100%",
  },
]);

const adviceOptions = ref([
  {
    value: "<999",
    label: "不限",
  },
  {
    value: ">0",
    label: "增开",
  },
  {
    value: "=0",
    label: "不变",
  },
  {
    value: "<0",
    label: "合并",
  },
]);

const formData = ref({
  dateRange: [new Date(), new Date()],
  advice: "<999",
  rateRangeZUUU: "0,100",
  rateRangeZUTF: "0,100",
});

const queryParameters = ref(null);
const queryResult = ref([]);

const getQueryParameters = () => {
  let params = {
    dateRange: [
      dayjs(formData.value.dateRange[0]).format("YYYY-MM-DD") + " 00:00:00",
      dayjs(formData.value.dateRange[1]).format("YYYY-MM-DD") + " 23:59:59",
    ],
    advice: formData.value.advice,
    rateRangeZUUU: formData.value.rateRangeZUUU.split(","),
    rateRangeZUTF: formData.value.rateRangeZUTF.split(","),
  };
  queryParameters.value = params;
  return params;
};

const handleQuery = () => {
  let params = getQueryParameters();
  queryResult.value = [];
  axios
    .post("api/note_flow_query.php", params)
    .then((response) => {
      // ElMessageBox({
      //   title: "Response",
      //   message: h("div", null, JSON.stringify(response.data, null, 2)),
      // });
      if (response.data.success) {
        queryResult.value = response.data.result;
        ElNotification({
          title: `查找到${response.data.result.length}条记录`,
          type: "success",
        });
      } else {
        ElNotification({
          title: "未查找到任何内容",
          type: "warning",
        });
      }
    })
    .catch((error) => {
      ElNotification({
        title: "出错了",
        message: error.message,
        type: "warning",
      });
    });
};

onMounted(() => {
  getQueryParameters();
});
</script>
